package cn.zjj.system.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.org.domain.Department;
import cn.zjj.system.domain.Menu;
import cn.zjj.system.domain.Role;
import cn.zjj.system.mapper.MenuMapper;
import cn.zjj.system.mapper.RoleMapper;
import cn.zjj.system.service.IMenuService;
import cn.zjj.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insert(Role role) {

        roleMapper.insert(role);//返回id
        saveLinkForm(role);
    }
    private void saveLinkForm(Role role){
        if(role.getMenus().size()>0){//如果传来的菜单有id数据
            roleMapper.insertRoleMenu(role.getId(),role.getMenus());
        }
        if(role.getPermissions().size()>0){
            roleMapper.insertRolePermission(role.getId(),role.getPermissions());
        }
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);

        //先删除中间表 ,再一次性添加中间表  根据当前的角色id 删除  employeerole这个不变,只是角色的权限之类发生改变,员工还是那个角色
        roleMapper.deleteRoleMenu(role.getId());
        roleMapper.deleteRolePermission(role.getId());

        saveLinkForm(role);

    }

    @Override
    public void delete(Long id) {
        //先删除关联表
        roleMapper.deleteRoleMenu(id);
        roleMapper.deleteRolePermission(id);
        //还应该删除employee role这个中间表,角色都没了,这个也该没
        roleMapper.deleteEmployeeRole(id);
        //再删除role表
        roleMapper.delete(id);
    }
}
